import 'package:flutter/material.dart';
import 'package:open_file/open_file.dart';
import '../../helper/downloadHelper.dart';
import '../../helper/iconHelper.dart';
import '../../helper/uidata.dart';
import '../../spaces/filespace/fileSpace.dart';
import '../../spaces/filespace/fileSpace_helper.dart';
import '../../spaces/filespace/file_view.dart';
import '../../model/space_item.dart';
import '../../spaces/image_space/recod_image_scan.dart';
import '../../spaces/image_space/record_list.dart';
import '../../spaces/movie_space/movie_play.dart';
import '../../spaces/movie_space/movie_play_scan.dart';
import '../../widgets/ImgWidget.dart';
import '../../widgets/dialogWidget.dart';
import '../../widgets/inputWidget.dart';

//import 'dialogWidget.dart';

import '../../helper/cacheHelper.dart';
import '../../helper/Helper.dart';
import '../../helper/global_config.dart';
import '../../helper/routeHelper.dart';

import '../../model/user.dart';
import 'record_list.dart';
//import 'ImgWidget.dart';

class RecordCardHelper {
  static Widget cardHeader(BuildContext context, User u1) {
    if (u1 == null) return new Container();
    return GestureDetector(
        // onTap: () => RouteHelper.UserInfo(context, u1),
        child: Container(
      child: new Row(
        children: <Widget>[
          new Container(
            child: new CircleAvatar(
              backgroundColor: Colors.blue,
              radius: 8.0,
            ),
            margin: EdgeInsets.only(right: 6.0),
          ),
          u1.Name != null
              ? new Container(
                  child: new Text(
                    u1.Name,
                    style: TextStyle(
                      // color: GlobalConfig.fontColor,
                      fontSize: 16.0,
                    ),
                  ),
                  margin: EdgeInsets.only(right: 12.0),
                )
              : new Container(),
        ],
      ),
      padding: const EdgeInsets.only(top: 10.0),
    ));
  }

  static Widget cardRemark(SpaceItem r1) {
    return new Container(
        child: r1.Remarks == null
            ? null
            : new Text(r1.Remarks,
                style:
                    new TextStyle(height: 1.3, color: GlobalConfig.fontColor)),
        margin: const EdgeInsets.only(top: 6.0),
        alignment: Alignment.topLeft);
  }

  static Widget cardTime(BuildContext ctx, SpaceItem r1) {
    var t1 = DateTime.parse(r1.CreateTime);
    return new ListTile(
      title: Text(t1 != null ? Helper.DateTimeToString(t1) : "",
          style: InputWidget.subTxtStyle2),
      trailing: IconButton(
        icon: Icon(Icons.fullscreen),
        onPressed: () => RouteHelper.ShowPage(ctx, MovieScanPage(r1.Guid)),
      ),
    );
    //padding: const EdgeInsets.only(bottom: 8.0),
    //alignment: Alignment.topLeft);
  }

  static Widget cardTime2(BuildContext ctx, SpaceItem r1) {
    var t1 = DateTime.parse(r1.CreateTime);
    return new ListTile(
      title: Text(t1 != null ? Helper.DateTimeToString(t1) : "",
          style: InputWidget.subTxtStyle2),

      trailing: Text(
        Helper.GetFileSize(r1.Size),
        style: TextStyle(color: Colors.black38, fontSize: 14.0),
      ),
      // trailing: IconButton(icon:Icon( Icons.fullscreen),onPressed: ()=>RouteHelper.ShowPage(ctx, MovieScanPage(r1.Guid)),),
    );
    //padding: const EdgeInsets.only(bottom: 8.0),
    //alignment: Alignment.topLeft);
  }

  static Widget cardImg(
      BuildContext context, SpaceItem r1, RecordDeleteCallback deleted) {
    var u1 = CacheHelper.GetUser(r1.UserId);
    return GestureDetector(
        onTap: () {
          RouteHelper.ShowPage(context, RecordImageScanPage(r1));
        },
        onLongPress: () {
          if (u1 != null && u1.Guid != r1.UserId) {
            DialogWidget.getAlert(context, "提示", "只能删除自己上传的记录");
          } else {
            showDialog(
              context: context,
              builder: (BuildContext context) {
                return DialogWidget.getDialog(context, "删除照片", "点击确定键将删除该张照片！",
                    () {
                  SpaceItem.Delete(r1);
                  deleted(r1);
                });
              },
            );
          }
        },
        child: new Container(
            child: new AspectRatio(
              aspectRatio: 1.5 / 1.0,
              child: ImageWidget(r1.Guid),
            ),
            margin: new EdgeInsets.only(top: 6.0, bottom: 8.0),
            alignment: Alignment.topLeft));
  }

  static Widget cardMovie(
      BuildContext context, SpaceItem r1, RecordDeleteCallback deleted) {
    var u1 = CacheHelper.GetUser(r1.UserId);
    return GestureDetector(
        onTap: () {
          //RouteHelper.ShowPage(context, RecordImageScanPage(r1));
        },
        onLongPress: () {
          if (u1 != null && u1.Guid != r1.UserId) {
            DialogWidget.getAlert(context, "提示", "只能删除自己上传的记录");
          } else {
            showDialog(
              context: context,
              builder: (BuildContext context) {
                return DialogWidget.getDialog(context, "删除照片", "点击确定键将删除该张照片！",
                    () {
                  SpaceItem.Delete(r1);
                  deleted(r1);
                });
              },
            );
          }
        },
        child: Container(
            height: 200.0, child: Center(child: MoviePlayPage(r1.Guid))));
  }

  static Widget ImgCard(
      BuildContext context, SpaceItem record, RecordDeleteCallback onDelete) {
    // var t1 = DateTime.parse(record.CreateTime);
    var u1 = CacheHelper.GetUser(record.UserId);

    return new Container(
        //margin: const EdgeInsets.only(top: 5.0, bottom: 5.0),
        key: Key(record.Guid),
        margin: const EdgeInsets.only(top: 5.0, bottom: 5.0),
        color: GlobalConfig.cardBackgroundColor,
        child: new Container(
          margin: new EdgeInsets.all(8.0),
          padding: new EdgeInsets.only(left: 4.0, right: 4.0),
          // onPressed: () {},
          child: new Column(
            children: <Widget>[
              cardHeader(context, u1),
              cardRemark(record),
              cardImg(context, record, onDelete),
              cardTime2(context, record),
            ],
          ),
        ));
  }

  static Widget MovCard(
      BuildContext context, SpaceItem record, RecordDeleteCallback onDelete) {
    // var t1 = DateTime.parse(record.CreateTime);
    var u1 = CacheHelper.GetUser(record.UserId);

    return new Container(
        //margin: const EdgeInsets.only(top: 5.0, bottom: 5.0),
        key: Key(record.Guid),
        margin: const EdgeInsets.only(top: 5.0, bottom: 5.0),
        color: GlobalConfig.cardBackgroundColor,
        child: new Container(
          margin: new EdgeInsets.all(8.0),
          padding: new EdgeInsets.only(left: 4.0, right: 4.0),
          // onPressed: () {},
          child: new Column(
            children: <Widget>[
              cardHeader(context, u1),
              cardRemark(record),
              cardMovie(context, record, onDelete),
              //MyImg(record.ImgId),
              // cardLocation(context, record),

              cardTime(context, record),
            ],
          ),
        ));
  }

  static Widget cardFile(
      BuildContext context, SpaceItem s1, FileDeleteCallback deleted) {
    var u1 = CacheHelper.GetUser(s1.UserId);
    String _remark = Helper.DateTimeFormat2(s1.CreateTime);
    if (u1 != null) _remark = _remark + "    " + u1.Name;
    return Container(
        key: Key(s1.Guid),
        color: Colors.white,
        child: Column(children: [
          Container(
            margin: const EdgeInsets.only(top: 3.0, bottom: 3.0),
            child: ListTile(
              title: Text(s1.FileName),
              subtitle: Padding(
                padding: EdgeInsets.only(top: 4.0),
              ),
              leading: IconHelper.getFileIcon(s1.FileName, 36.0),
              onTap: () async {
                var fPath = await DownloadHelper.GetLocalFile(s1.Guid);
                if (fPath != "") {
                  if (FileSpaceHelper.IfView(s1.FileName)) {
                    RouteHelper.ShowPage(
                        context,
                        FileReaderPage(
                          filePath: fPath,
                        ));
                  } else {
                    OpenFile.open(fPath);
                  }
                } else {
                  showDialog(
                    context: context,
                    builder: (BuildContext context) {
                      return DialogWidget.getDialog(
                          context, "下载文件", "点击确定将该文件添加到下载目录！", () {
                        DownloadHelper.DownLoad2(context, s1);
                      });
                    },
                  );
                }
              },
              onLongPress: () {
                showDialog(
                  context: context,
                  builder: (BuildContext context) {
                    if (u1.Guid != UIData.CurrentUser!.Guid) {
                      return DialogWidget.getAlert(
                          context, "提示", "只能删除自己上传的文件");
                    } else {
                      return DialogWidget.getDialog(
                          context, "删除文件", "点击确定将从服务器中删除该文件！", () {
                        SpaceItem.Delete(s1);
                        deleted(s1);
                      });
                    }
                  },
                );
              },
            ),
          ),
        ]));
  }

  static Widget FileCard(
      BuildContext context, SpaceItem record, RecordDeleteCallback onDelete) {
    // var t1 = DateTime.parse(record.CreateTime);
    var u1 = CacheHelper.GetUser(record.UserId);

    return new Container(
        //margin: const EdgeInsets.only(top: 5.0, bottom: 5.0),
        key: Key(record.Guid),
        margin: const EdgeInsets.only(top: 5.0, bottom: 5.0),
        color: GlobalConfig.cardBackgroundColor,
        child: new Container(
          margin: new EdgeInsets.all(8.0),
          padding: new EdgeInsets.only(left: 4.0, right: 4.0),
          // onPressed: () {},
          child: new Column(
            children: <Widget>[
              cardHeader(context, u1),
              cardRemark(record),
              cardFile(context, record, onDelete),
              //MyImg(record.ImgId),
              // cardLocation(context, record),
              cardTime2(context, record),
            ],
          ),
        ));
  }
}
